package org.whb.quartz.service.impl;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;
import org.whb.quartz.job.MainJob;
import org.whb.quartz.service.IQuartzService;
import org.whb.quartz.utils.QuartzJobInfo;
import org.whb.quartz.utils.QuartzUtils;

import java.util.Date;
import java.util.Map;

@Service
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    @Override
    public void addJob(QuartzJobInfo info) {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        String jobName = info.getJobName();
        Map<String, Object> params = info.getParams();
        String cronj = info.getCronj();
        QuartzUtils.addJob(scheduler, jobName, MainJob.class, params, cronj);
        System.out.println("定时任务："+jobName+"添加成功！！！");

    }

    @Override
    public void removeJob(String jobName) {
        Scheduler scheduler = schedulerFactoryBean.getScheduler();
        QuartzUtils.removeJob(scheduler, jobName);

        System.out.println("定时任务："+jobName+"已移除");
    }


}
